﻿using LibNiconico.NiconicoApi.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Mylist
{
    /// <summary>
    /// ニコニコマイリスト追加リクエストレスポンス
    /// </summary>
    public sealed class NicoMylistAddRequestResponse
    {
        private NicoMylistAddRequestResponse(XDocument document)
        {
            mylistItems = new List<NicoMylistItem>();
            ItemId = "";
            ItemType = "";
            Token = "";

            try
            {
                var items = from element in document.Descendants("optgroup")
                            where element.Attribute("label").Value == "個別マイリスト"
                            select element;

                if (items.Count() > 0)
                {
                    var myListItems = from element in items.First().Elements()
                                      where element.Attribute("value") != null && element.Value != null
                                      select new NicoMylistItem(element.Attribute("value").Value, element.Value);
                    mylistItems.AddRange(myListItems);
                }


                var itemTypes = from element in document.Descendants("input")
                                where element.Attribute("name") != null && element.Attribute("name").Value == "item_type" && element.Attribute("value") != null
                                select element.Attribute("value").Value;

                if (itemTypes.Count() > 0) ItemType = itemTypes.First();

                var itemIds = from element in document.Descendants("input")
                                where element.Attribute("name") != null && element.Attribute("name").Value == "item_id" && element.Attribute("value") != null
                                select element.Attribute("value").Value;

                if (itemIds.Count() > 0) ItemId = itemIds.First();

                var nakidScripts = from element in document.Descendants("script")
                                   where element.Attribute("src") == null && element.Attribute("type") != null && element.Attribute("type").Value == "text/javascript" 
                                   select element;
                if (nakidScripts.Count() > 0)
                {


                    var tokens = from element in nakidScripts
                                 where
                                 element.Value != null && nicoApiTokenRegex.Match(element.FirstNode.ToString()).Success
                                 select nicoApiTokenRegex.Match(element.FirstNode.ToString()).Groups[1].Value;

                    if (tokens.Count() > 0) Token = tokens.First();

                }
            }
            catch (Exception e)
            {
                _eChancel(e);
            }
            

        }

        internal static NicoMylistAddRequestResponse Parse(XDocument document)
        {
            return new NicoMylistAddRequestResponse(document);
        }

        private static void _eChancel(Exception e)
        {

        }

        private List<NicoMylistItem> mylistItems;

        /// <summary>
        /// マイリストアイテムリスト
        /// </summary>
        public IEnumerable<NicoMylistItem> MylistItems { get { return mylistItems; } }

        /// <summary>
        /// アイテムID
        /// </summary>
        public string ItemId{ get;private set;}

        /// <summary>
        /// トークン
        /// </summary>
        public string Token { get; private set; }

        /// <summary>
        /// アイテムタイプ
        /// </summary>
        public string ItemType { get; private set; }

        private static readonly Regex nicoApiTokenRegex = new Regex("NicoAPI.token\\s*\\=\\s*\\\"([^\\\"]+)\\\";");
    }
}
